package com.aabte.auth.role.infrastructure.persistence;

import com.aabte.auth.role.domain.model.Role;
import com.aabte.auth.role.domain.model.RoleId;
import com.aabte.auth.role.domain.repository.RoleRepository;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.UUID;

@Repository
public class RoleRepositoryHibernate implements RoleRepository {

    @Resource
    private RoleDAO roleDAO;

    @Override
    public RoleId nextRoleId() {
        return new RoleId(UUID.randomUUID().toString().replaceAll("-", ""));
    }

    @Override
    public void addOrUpdate(Role role) {
        roleDAO.save(role);
    }

    @Override
    public Optional<Role> find(RoleId roleId) {
        return roleDAO.findByRoleId(roleId);
    }

    @Override
    public void remove(Role role) {
        roleDAO.delete(role);
    }
}
